﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text.RegularExpressions;
using Higer.SharePoint.HigerWebService;
using Dextrys.SharePoint;
namespace MyBullitin
{
    public partial class MyBulltin :BaseUserControl
    {
        #region  属性
        /// <summary>
        /// 公告数量设置
        /// </summary>
        private int _ListNumber = 10;

        [WebBrowsable(true)]
        [WebDisplayName("公告显示数量")]
        [Personalizable(PersonalizationScope.User)] //设置个性化
        public int ListNumber
        {
            get { return _ListNumber; }
            set { _ListNumber = value; }
        }

        /// <summary>
        /// 设置 显示 new 图标的未查看公告的天数
        /// </summary>
        private int _ShowNewImageDays = 3;
        [WebBrowsable(true)]
        [WebDisplayName("显示未查看公告标识的天数")]
        [Personalizable(PersonalizationScope.User)] //设置个性化
        public int ShowNewImageDays
        {
            get { return _ShowNewImageDays; }
            set { _ShowNewImageDays = value; }
        }



        public enum InternetType
        {
            局域网,
            互联网
        }


        InternetType _HigerNetType = InternetType.局域网;

        [WebBrowsable(true)]
        [WebDisplayName("网络类型")]
        [Personalizable(true)] //设置个性化
        public InternetType HigerNetType
        {
            get { return _HigerNetType; }
            set { _HigerNetType = value; }
        }

        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //是否显示 历次 按钮
                this.HideOrShowHistoryLink();

                string url = ConfigurationManager.AppSettings["HistoryBulltinUrl"].ToString();
               // string loginUser = this.GetLoginUser();
                string loginUser = "xuxt";

                url = url.Replace("@@logonid", loginUser);

                this.SetHistoryLinkUrl(url);
                if (!this.IsPostBack)
                {


                    HigerWorkList worklist = new HigerWorkList();
                    //  DataSet ds = worklist.GetBulltins(this.GetLoginUser(), HigerWorkList.emBulltinsType.有效);
                    DataSet ds = worklist.GetBulltins("xuxt", HigerWorkList.emBulltinsType.全部);

                    if (ds == null || ds.Tables.Count == 0)
                    {
                        return;
                    }
                    DataTable dt = ds.Tables[0];
                    int bulltinCount = dt.Rows.Count;
                    //显示条数
                    this.grdBullitins.PageSize = this._ListNumber;
                    //排序设置
                    dt.DefaultView.Sort = "content_id desc";
                    this.grdBullitins.DataSource = dt.DefaultView.ToTable();
                    this.grdBullitins.DataBind();

                    int showCount = 0;
                    if (this.grdBullitins.Rows.Count > this._ListNumber)
                    {
                        showCount = _ListNumber;
                    }
                    else
                    {
                        showCount = this.grdBullitins.Rows.Count;
                    }


                    //设置 gridview 样式、内容
                    for (int i = 0; i < showCount; i++)
                    {
                        Image newImage = ((Image)this.grdBullitins.Rows[i].Cells[1].FindControl("imgNew"));
                        newImage.ImageUrl = "Resource/new.gif";
                        if (dt.Rows[i]["is_sign"].ToString().Trim().ToUpper() == "N")
                        {
                            newImage.Visible = true;
                        }
                        else
                        {
                            newImage.Visible = false;
                        }
                        //              this.grdBullitins.Rows[i].Cells[3].Text = this.ShowDate(DateTime.Parse(this.grdBullitins.Rows[i].Cells[3].Text));

                    }

                    //判断是否需要显示“更多按钮”
                    //实际公数量 大于 用户设置数 则显示 ，否则隐藏
                    this.HideOrShowMoreLink(bulltinCount);
                }
            }
            catch
            {
            }

        }

        protected void grdBullitins_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {

                if (e.CommandName == "HiddenPostButtonCommand")
                {
                    Control cmdControl = e.CommandSource as Control;
                    GridViewRow row = cmdControl.NamingContainer as GridViewRow; // 当前行

                    //holly add 2009/09/11
                    //回写函数在哪？？？？？？？？
                    //隐藏 new 图标
                    Image newImage = ((Image)(this.grdBullitins.Rows[row.RowIndex].Cells[1].FindControl("imgNew")));
                    if (newImage.Visible)
                    {
                        string strContentID = this.grdBullitins.Rows[row.RowIndex].Cells[0].Text.Trim();
                        HigerWorkList workList = new HigerWorkList();
                      //  bool bReturn = workList.WriteReadContentRecord(this.GetLoginUser(), strContentID);
                        bool bReturn = workList.WriteReadContentRecord("xuxt", strContentID);

                    }
                    newImage.Visible = false;

                }
            }
            catch
            {
            }
        }


        /// <summary>
        /// 数据绑定加载回写事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBullitins_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowIndex > -1)
                {
                    Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
                    if (btnHiddenPostButton != null)
                    {

                        //用于触发 gridview的onrowcommand事件
                        ((Label)e.Row.Cells[1].FindControl("lblTitle")).Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);

                    }

                }
            }
            catch
            {
            }
        }
        #endregion

        #region 方法

        /// <summary>
        /// 设置是否显示 “更多”链接接
        /// </summary>
        /// <param name="validBulltins">有效公告数</param>
        private void HideOrShowMoreLink(int validBulltins)
        {
            if (validBulltins > this._ListNumber)
            {
                this.linkMore.Visible = true;
            }
            else
            {
                this.linkMore.Visible = false;
            }
        }
        /// <summary>
        /// 设置“历次”的 查询链接
        /// </summary>
        /// <param name="url"></param>
        private void SetHistoryLinkUrl(string url)
        {
            this.linkHistory.NavigateUrl = url;
            this.linkHistory.Target = "_blank";
        }
        /// <summary>
        /// 显示日期 以 mm/dd的形式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private string ShowDate(DateTime date)
        {
            if (date == null)
            {
                return string.Empty;
            }

            string strMonth = date.Month.ToString();
            if (strMonth.Length < 2)
            {
                strMonth = "0" + strMonth;
            }
            string strDay = date.Day.ToString();
            if (strDay.Length < 2)
            {
                strDay = "0" + strDay;
            }
            return strMonth + "/" + strDay;
        }
        /// <summary>
        /// 根据用户网络类型，显示或隐藏 “历次” 按钮
        /// </summary>
        private void HideOrShowHistoryLink()
        {
            if (this._HigerNetType == InternetType.局域网)
            {
                this.linkHistory.Visible = true;
            }
            else
            {
                this.linkHistory.Visible = false;
            }
        }

        /// <summary>
        /// 获取当前登陆用户
        /// </summary>
        /// <returns></returns>
        private string GetLoginUser()
        {
            SPWeb web = SPControl.GetContextWeb(this.Context);
            string loginName = web.CurrentUser.LoginName;
            string[] userName = loginName.Split('\\');
            if(userName.Length<2)
            {
                return userName[0];
            }

            return userName[1];
         //   return web.CurrentUser.LoginName;
        }
        #endregion


    }
}